package GUI.Lisenting;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//这种方法比较面向过程，还有更加面向过程的方法.
public class D04 {
    public static void main(String[] args) {
        new Calculator();
    }
}
//计算器类
class Calculator extends Frame{
    public Calculator() {
        //3个文本框
        TextField field1 = new TextField(10);
        TextField field2 = new TextField(10);
        TextField field3 = new TextField(20);
        //1个按钮
        Button button = new Button("=");
        button.addActionListener(new MyCalculatorListener(field1,field2,field3));
        //1个标签
        Label label = new Label("+");
        //布局
        setLayout(new FlowLayout());
        add(field1);
        add(label);
        add(field2);
        add(button);
        add(field3);
        pack();
        setVisible(true);
    }
}
//监听器类
class MyCalculatorListener implements ActionListener{
    //可以组合，略
    //Calculator calculator = null;
    private TextField num1,num2,num3;
    public MyCalculatorListener(TextField num1,TextField num2,TextField num3) {
        this.num1=num1;
        this.num2=num2;
        this.num3=num3;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        //1.获取加数和被加数(涉及强制转换)
        int n1 =Integer.parseInt(num1.getText());
        int n2 =Integer.parseInt(num2.getText());
        //2.加法运算放入第三框
        num3.setText(""+(n1+n2));
        //3.清除前两个框
        num1.setText("");
        num2.setText("");
    }
}
